#pragma once

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsTestX.h"
#include "QWidgetsGenerate.h"
#include "QtWidgetsAbout.h"

#include <qthread.h>
#include <qboxlayout.h>
#include <qgroupbox.h>
#include <qcombobox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qevent.h>
#include <qsqldatabase.h>
#include <qprogressbar.h>

class QtWidgetsTestX : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsTestX(QWidget* parent = nullptr);
    ~QtWidgetsTestX();

private:
    //Ui::QtWidgetsTestXClass ui;

    QVBoxLayout* m_leftLayout;
    QVBoxLayout* m_middleLayout;
    QVBoxLayout* m_rightLayout;

    QLabel* m_devNameInfoLabel;
    QLabel* m_devModelInfoLabel;
    QLabel* m_devInputInfoLabel;
    QLabel* m_devOutputInfoLabel;
    QLabel* m_devEnclosureInfoLabel;

    QComboBox* m_serialPortNameComboBox;
    QComboBox* m_serialBuadRateComboBox;
    QComboBox* m_serialDataBitsComboBox;
    QComboBox* m_serialStopBitsComboBox;
    QComboBox* m_serialParityComboBox;
    QComboBox* m_serialFlowCtrlComboBox;
    QPushButton* m_serialOnOffPushButton;
    QPushButton* m_serialRefreshPushButton;

    QLabel* m_wifiRssiInfoLabel;
    QLabel* m_wifiTempInfoLabel;
    QLabel* m_wifiFwVerInfoLabel;
    QLabel* m_wifiHwVerInfoLabel;
    QLabel* m_wifiMacInfoLabel;

    QLabel* m_dspOutputStateLabel;
    QLabel* m_dspPV1InputVLabel;
    QLabel* m_dspPV1InputCLabel;
    QLabel* m_dspPV2InputVLabel;
    QLabel* m_dspPV2InputCLabel;
    QLabel* m_dspPV3InputVLabel;
    QLabel* m_dspPV3InputCLabel;
    QLabel* m_dspPV4InputVLabel;
    QLabel* m_dspPV4InputCLabel;
    QLabel* m_dspInputPowerLabel;
    QLabel* m_dspAPhaseOutputVLabel;
    QLabel* m_dspAPhaseOutputCLabel;
    QLabel* m_dspAPhaseOutputVFreqLabel;
    QLabel* m_dspPowerValLabel;
    QLabel* m_dspTempValLabel;
    QLabel* m_dspFailureCodeLabel;
    QLabel* m_dspFwVerLabel;
    QLabel* m_dspHwVerLabel;
    QLabel* m_dspOutputCUpValLabel;
    QLabel* m_dspOutputVFreqUpDnValLabel;
    QLabel* m_dspOutputVUpDnValLabel;
    QLabel* m_dspInputVUpDnValLabel;
    QLabel* m_dspInputCUpValLabel;
    QLabel* m_dspStartTimeLabel;
    QLabel* m_dspTPUpValLabel;
    QLabel* m_dspOutputOverVolProtectTimeAdjustLabel;
    QLabel* m_dspOutputUnderVolProtectTimeAdjustLabel;
    QLabel* m_dspActivePowerRegulationLabel;
    QLabel* m_dspReactivePowerRegulationLabel;
    QLabel* m_dspPFCRegulationLabel;
    QLabel* m_dspACPhaseAdjustLabel;
    QLabel* m_dspOverUnderFreqProtectTimeAdjustLabel;
    QLabel* m_dspInputVKBLabel;
    QLabel* m_dspInputCKBLabel;
    QLabel* m_dspOutputVKBLabel;
    QLabel* m_dspOutputCKBLabel;
    QLabel* m_dspSnLabel;
    QLabel* m_dspBPhaseOutputVLabel;
    QLabel* m_dspBPhaseOutputCLabel;
    QLabel* m_dspBPhaseOutputVFreqLabel;
    QLabel* m_dspCPhaseOutputVLabel;
    QLabel* m_dspCPhaseOutputCLabel;
    QLabel* m_dspCPhaseOutputVFreqLabel;
    QLabel* m_dspRunModeLabel;

    QLineEdit* m_cfgOutputCUpValLineEdit;
    QPushButton* m_cfgOutputCUpValCommitPushButton;
    QLineEdit* m_cfgOutputVFreqUpValLineEdit;
    QLineEdit* m_cfgOutputVFreqDnValLineEdit;
    QPushButton* m_cfgOutputVFreqUpDnValCommitPushButton;
    QLineEdit* m_cfgOutputVUpValLineEdit;
    QLineEdit* m_cfgOutputVDnValLineEdit;
    QPushButton* m_cfgOutputVUpDnValCommitPushButton;
    QLineEdit* m_cfgInputVUpValLineEdit;
    QLineEdit* m_cfgInputVDnValLineEdit;
    QPushButton* m_cfgInputVUpDnValCommitPushButton;
    QLineEdit* m_cfgInputCUpValLineEdit;
    QPushButton* m_cfgInputCUpValCommitPushButton;
    QLineEdit* m_cfgStartTimeLineEdit;
    QPushButton* m_cfgStartTimeCommitPushButton;
    QLineEdit* m_cfgTPUpValLineEdit;
    QPushButton* m_cfgTPUpValCommitPushButton;

    QLineEdit* m_cfgOutputOverVolProtectTimeAdjustLineEdit;
    QPushButton* m_cfgOutputOverVolProtectTimeAdjustCommitPushButton;
    QLineEdit* m_cfgOutputUnderVolProtectTimeAdjustLineEdit;
    QPushButton* m_cfgOutputUnderVolProtectTimeAdjustCommitPushButton;
    QLineEdit* m_cfgActivePowerRegulationLineEdit;
    QPushButton* m_cfgActivePowerRegulationtCommitPushButton;
    QLineEdit* m_cfgReactivePowerRegulationLineEdit;
    QPushButton* m_cfgReactivePowerRegulationtCommitPushButton;
    QLineEdit* m_cfgPFCRegulationLineEdit;
    QPushButton* m_cfgPFCCommitPushButton;
    QLineEdit* m_cfgACPhaseAdjustLineEdit;
    QPushButton* m_cfgACPhaseAdjustCommitPushButton;
    QLineEdit* m_cfgOverUnderFreqProtectTimeAdjustEdit;
    QPushButton* m_cfgOverUnderFreqProtectTimeAdjustCommitPushButton;
    QLineEdit* m_cfgSnLineEdit;
    QPushButton* m_cfgSnCommitPushButton;
    QLineEdit* m_cfgInputRefV1LineEdit;
    QLineEdit* m_cfgOutputRefV1LineEdit;
    QPushButton* m_cfgReadRefV1PushButton;
    QLineEdit* m_cfgInputRefV2LineEdit;
    QLineEdit* m_cfgOutputRefV2LineEdit;
    QPushButton* m_cfgReadRefV2PushButton;
    QLineEdit* m_cfgInputRefA1LineEdit;
    QLineEdit* m_cfgOutputRefA1LineEdit;
    QPushButton* m_cfgReadRefA1PushButton;
    QLineEdit* m_cfgInputRefA2LineEdit;
    QLineEdit* m_cfgOutputRefA2LineEdit;
    QPushButton* m_cfgReadRefA2PushButton;
    QPushButton* m_cfgCalcRefVPushButton;
    QPushButton* m_cfgCalcRefAPushButton;
    QLineEdit* m_cfgInputVKLineEdit;
    QLineEdit* m_cfgInputVBLineEdit;
    QPushButton* m_cfgInputVKBCommitPushButton;
    QLineEdit* m_cfgInputCKLineEdit;
    QLineEdit* m_cfgInputCBLineEdit;
    QPushButton* m_cfgInputCKBCommitPushButton;
    QLineEdit* m_cfgOutputVKLineEdit;
    QLineEdit* m_cfgOutputVBLineEdit;
    QPushButton* m_cfgOutputVKBCommitPushButton;
    QLineEdit* m_cfgOutputCKLineEdit;
    QLineEdit* m_cfgOutputCBLineEdit;
    QPushButton* m_cfgOutputCKBCommitPushButton;

    QPushButton* m_ctrlRsdOnPushButton;
    QPushButton* m_ctrlRsdOffPushButton;
    QLineEdit* m_ctrlOtaUrlLineEdit;
    QPushButton* m_ctrlOtaDownloadPushButton;
    QLineEdit* m_ctrlOtaPathLineEdit;
    QPushButton* m_ctrlOtaBroserPushButton;
    QProgressBar* m_ctrlOtaUpdateProgressBar;
    QPushButton* m_ctrlOtaUpdatePushButton;

    QThread* m_serialPortThread;
    QThread* m_serialReadWriteThread;

    QSqlDatabase m_sqlDatabase;

    bool m_sqlDatabaseIsOk{ false };
    bool m_serialPortIsOk{ false };

    void layoutInit();

    void menuLayoutInit();

    void leftLayoutInit();
    void leftDevLayoutInit();
    void leftSerialLayoutInit();

    void middleLayoutInit();
    void middleWifiLayoutInit();
    void middleDspLayoutInit();

    void rightLayoutInit();
    void rightCfgLayoutInit();
    void rightCtrlLayoutInit();

    void closeEvent(QCloseEvent* event);

signals:
    void sig_devTypeComboBoxChanged(int index);

    void sig_serialPortNameChanged(const QString& text);
    void sig_serialOpen();
    void sig_serialClose();

    void sig_serialDataSend(const quint8 cmd, const QByteArray& data);
    void sig_otaDev_triggered(QString filePath);

private slots:
    void on_actionGenerateBinFile_triggered();
    void on_actionAbout_triggered();

    void on_serialPortNameComboBox_changed(const QString& text);
    void on_serialStateChanged(const bool& flag);

    void on_serialOnOffPushButton_clicked();
    void on_serialRefreshPushButton_clicked();

    void on_cfgStartTimeCommitPushButton_clicked();
    void on_cfgOutputCUpValCommitPushButton_clicked();
    void on_cfgInputCUpValCommitPushButton_clicked();
    void on_cfgTPUpValCommitPushButton_clicked();
    void on_cfgOutputVFreqUpDnValCommitPushButton_clicked();
    void on_cfgOutputVUpDnValCommitPushButton_clicked();
    void on_cfgInputVUpDnValCommitPushButton_clicked();

    void on_cfgOutputOverVolProtectTimeAdjustCommitPushButton_clicked();
    void on_cfgOutputUnderVolProtectTimeAdjustCommitPushButton_clicked();
    void on_cfgActivePowerRegulationtCommitPushButton_clicked();
    void on_cfgReactivePowerRegulationtCommitPushButton_clicked();
    void on_cfgPFCCommitPushButton_clicked();
    void on_cfgACPhaseAdjustCommitPushButton_clicked();
    void on_cfgOverUnderFreqProtectTimeAdjustCommitPushButton_clicked();

    void on_cfgReadRefV1PushButton_clicked();
    void on_cfgReadRefV2PushButton_clicked();
    void on_cfgReadRefA1PushButton_clicked();
    void on_cfgReadRefA2PushButton_clicked();
    void on_cfgCalcRefVPushButton_clicked();
    void on_cfgCalcRefAPushButton_clicked();
    void on_cfgInputVKBCommitPushButton_clicked();
    void on_cfgInputCKBCommitPushButton_clicked();
    void on_cfgOutputVKBCommitPushButton_clicked();
    void on_cfgOutputCKBCommitPushButton_clicked();
    void on_cfgSnCommitPushButton_clicked();

    void on_ctrlRsdOnPushButton_clicked();
    void on_ctrlRsdOffPushButton_clicked();

    void on_ctrlOtaDownloadPushButton_clicked();
    void on_ctrlOtaBroserPushButton_clicked();
    void on_ctrlOtaUpdatePushButton_clicked();

    void on_cfgOtaUpdateProgressBar_valueChanged(int value);
    void on_cfgOtaUpdateResult_finished(QString result, bool state);

    void on_serialDataReceive(QByteArray data);

public slots:
    void on_devNameFromDialog_changed(const QList<QString>);
};
